package com.haople.sso.ram.authentication.impl;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.haople.sso.core.authentication.UserAuthorization;
import com.haople.sso.core.util.Md5;

/**
 * 验证用户授权
 * @author Administrator
 *
 */
public class RAMUserAuthorization implements UserAuthorization {

	/**
	 *  users
	 * 	k: userName
	 *  v: password(MD5) 主：此处为 MD5 加密的密文
	 */
	private Map<String, String> users=new ConcurrentHashMap<String, String>();
	
	/**
	 * 返回用户的标识
	 */
	public String authorization(String userName, String password) {
		String userKey=null;
		if(userName==null || userName.equals("")){
			return userKey;
		}
		if(password==null || password.equals("")){
			return userKey;
		}
		String passwd=users.get(userName);
		if(passwd==null || passwd.equals("")){
			return userKey;
		}
		if(Md5.md5(password).equals(passwd)){
			userKey= userName;
		}
		return userKey;
	}

	public void setUsers(Map<String, String> users) {
		this.users = users;
	}

	@Override
	public boolean exist(String userName) {
		if(users.get(userName)!=null) {
			return true;
		}
		return false;
	}

	@Override
	public boolean save(String userName, String password) {
		users.put(userName, Md5.md5(password));
		return true;
	}

	@Override
	public boolean changePassword(String userName, String newPassword) {
		users.put(userName, Md5.md5(newPassword));
		return true;
	}

}
